<?php

namespace app\api\controller;

use app\admin\model\WechatUser;
use app\service\WechatService;
use fast\Random;
use Overtrue\Socialite\AuthorizeFailedException;
use think\facade\Cookie;
use think\facade\Event;
use think\facade\Validate;
use think\facade\Config;
use app\common\library\Ems;
use app\common\library\Sms;
use app\common\controller\Api;

/**
 * 会员接口.
 */
class Wechat extends Api
{
    protected $noNeedLogin = ["*"];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
        $auth = $this->auth;
        //监听注册登录注销的事件
        Event::listen('user_login_successed', function ($user) use ($auth) {
            $expire = request()->param('keeplogin') ? 30 * 86400 : 0;
            Cookie::set('uid', $user->id, $expire);
            Cookie::set('token', $auth->getToken(), $expire);
        });
        Event::listen('user_register_successed', function ($user) use ($auth) {
            Cookie::set('uid', $user->id);
            Cookie::set('token', $auth->getToken());
        });
        Event::listen('user_delete_successed', function ($user) use ($auth) {
            Cookie::delete('uid');
            Cookie::delete('token');
        });
        Event::listen('user_logout_successed', function ($user) use ($auth) {
            Cookie::delete('uid');
            Cookie::delete('token');
        });
    }

    public function test(){
        print_r($_REQUEST);

       // app("wechat_service")->test();
    }

    public function getauthurl(){
        $callbackurl = $this->request->request("callbackurl");
        $user_type = $this->request->request("type",0);
      //  $user_type = $user_type?"snsapi_userinfo":"snsapi_base";
        $user_type = "snsapi_userinfo";
        $auth_data =  app("wechat_service")->getAuthUrl($callbackurl,$user_type);
        if($auth_data["status"] == "1"){
            $this->success("获取成功", $auth_data["data"]);
        }else{
            $this->error("获取失败", $auth_data["data"]);
        }
     //  return json($auth_data);
    }

    public function getuserinfo(){
            $user = app("wechat_service")->getUserInfo();
            if($user["status"] == "0"){
                return json($user);
            }
            $wechat_user = $user["data"];
            if(!$wechat_user->user_id){
                $user = \app\admin\model\User::where("username",$wechat_user->openid)->find();
                if($user){
                    $wechat_user->user_id = $user->id;
                    $wechat_user->save();
                }else{
                    $is_register =  $this->auth->register($wechat_user->openid,md5($wechat_user->openid.time().rand(1111,9999)));
                    if(!$is_register){
                        return json(["status"=>0,"msg"=>"用户注册失败","data"=>"GUI0001"]);
                    }
                    $user = $this->auth->getUser();
                    $user->nickname = $wechat_user->nickname;
                    $user->avatar = $wechat_user->headimgurl;
                    $user->save();
                    $wechat_user->user_id = $user->id;
                    $wechat_user->save();
                }
            }else{
               $is_login=  $this->auth->direct($wechat_user->user_id);
               if(!$is_login){
                   $this->error("用户登录失败", "GUI0002");
                 //  return json(["status"=>0,"msg"=>"用户登录失败","data"=>"GUI0002"]);
               }
                $user = $this->auth->getUser();
            }
            $this->success("登陆成功", ["user"=>$this->auth->getUserinfo(),"token"=>$this->auth->getToken()]);
          //  return json(["status"=>"获取成功","data"=>$this->auth->getUserinfo(),"token"=>$this->auth->getToken()]);
    }











}
